﻿using SharpCifs.Smb;
using System;

namespace CommonUtils
{
    public class SmbClient
    {
        public SmbFile Root;

        public SmbClient(string host)
        {
            //Logon failure: unknown user name or bad password.
            //Failed to connect, 0.0.0.0<00>/192.168.68.1  [ 0.0.0.0:50795 --> : ]
            Root = new SmbFile("smb://" + host + "/");

            //Logon failure: account currently disabled.
            //Root = new SmbFile("smb://" + host + "/Downloads/");

            //Access is denied.
            //Root = new SmbFile("smb://" + host + "/", NtlmPasswordAuthentication.Anonymous);d

            //Access is denied.
            //Root = new SmbFile("smb://" + host + "/Downloads/", NtlmPasswordAuthentication.Anonymous);

            //var guest = new NtlmPasswordAuthentication("?", "GUEST", string.Empty);

            //Logon failure: unknown user name or bad password.
            //Root = new SmbFile("smb://" + host + "/", guest);

            //Logon failure: account currently disabled.
            //Root = new SmbFile("smb://" + host + "/Downloads/", guest);
        }

        public SmbClient(string host, string root)
        {
            if (!root.EndsWith("/"))
                root += "/";
            Root = new SmbFile("smb://" + host + "/" + root);
        }

        public SmbClient(string host, string user, string pwd)
        {
            var auth = new NtlmPasswordAuthentication(user + ":" + pwd);
            Root = new SmbFile("smb://" + host + "/", auth);
        }

        public SmbClient(string host, string root, string user, string pwd)
        {
            if (!root.EndsWith("/"))
                root += "/";
            var auth = new NtlmPasswordAuthentication(user + ":" + pwd);
            Root = new SmbFile("smb://" + host + "/" + root, auth);
        }

        public string[] RootList()
        {
            try
            {
                return Root.List();
            }
            catch (Exception ex)
            {
                ex.Message.Print();
                return new string[] { ex.Message };
            }
        }

        public void CreateFolder(string name)
        {
            try
            {
                var folder = new SmbFile(Root, name);
                folder.Mkdir();
            }
            catch (Exception ex)
            {
                ex.Message.Print();
            }
        }
    }
}
